package com.ym.b2b.buyer.service.impl;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ym.b2b.buyer.dao.EmCBuyerAddressDao;
import com.ym.b2b.buyer.service.EmCBuyerAddressService;
import com.ym.b2b.buyer.sqlbean.EmCBuyerAddress;
import com.ym.b2b.commons.exception.BaseException;
import com.ym.b2b.commons.service.BaseService;
import com.zc.commons.base.Result;

/**
 * @ClassName: EmCBuyerAddressServiceImpl
 * @Description: TODO
 * @Author tangxj
 * @Date 2014-12-29 下午4:29:44
 * @Modify
 * @CopyRight B2B小组
 */

@Service
public class EmCBuyerAddressServiceImpl extends BaseService implements EmCBuyerAddressService {

    /*
     * (non-Javadoc)
     * @see com.ym.b2b.buyer.service.EmCBuyerAddressService#addBuyerAddress(com.ym.b2b.buyer.sqlbean.EmCBuyerAddress)
     */
    private Logger             logger = Logger.getLogger(getClass());
    @Autowired
    private EmCBuyerAddressDao emCBuyerAddressDao;

    @Transactional
    @Override
    public Result addBuyerAddress(EmCBuyerAddress emCBuyerAddress) throws BaseException {
        Result result = this.checkInputBuyerAddress(emCBuyerAddress);
        if (!result.getCode().equals("200")) return result;
        // Result result = new Result();
        try {
            Integer isDefault = emCBuyerAddress.getIsDefalut(); // 如果默认当前插入地址为默认收货地址，先更新其他地址为非默认收货地址
            if (isDefault == 1) {
                EmCBuyerAddress emCBuyerAddress2 = new EmCBuyerAddress();
                emCBuyerAddress2.setBuyerCode(emCBuyerAddress.getBuyerCode());
                emCBuyerAddress2.setIsDefalut(0);
                emCBuyerAddressDao.updateEmCBuyerAddressByBuyerCode(emCBuyerAddress2);
            }
            emCBuyerAddressDao.insertEmCBuyerAddress(emCBuyerAddress);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "添加收货地址失败!");
        }
        result.setResult("添加收货地址成功!");
        return result;
    }

    /*
     * (non-Javadoc)
     * @see com.ym.b2b.buyer.service.EmCBuyerAddressService#updateBuyerAddress(com.ym.b2b.buyer.sqlbean.EmCBuyerAddress)
     */
    @Override
    public Result updateBuyerAddress(EmCBuyerAddress emCBuyerAddress) throws BaseException {
        Result result = this.checkInputBuyerAddress(emCBuyerAddress);
        if (!result.getCode().equals("200")) return result;
        try {
            Integer isDefault = emCBuyerAddress.getIsDefalut(); // 如果默认当前插入地址为默认收货地址，先更新其他地址为非默认收货地址
            if (isDefault == 1) {
                EmCBuyerAddress emCBuyerAddress2 = new EmCBuyerAddress();
                emCBuyerAddress2.setBuyerCode(emCBuyerAddress.getBuyerCode());
                emCBuyerAddress2.setIsDefalut(0);
                emCBuyerAddressDao.updateEmCBuyerAddressByBuyerCode(emCBuyerAddress2);
            }
            emCBuyerAddressDao.updateEmCBuyerAddress(emCBuyerAddress);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "更新收货地址失败!");
        }
        result.setResult("更新收货地址成功!");
        return result;
    }

    /*
     * (non-Javadoc)
     * @see com.ym.b2b.buyer.service.EmCBuyerAddressService#deleteBuyerAddress(com.ym.b2b.buyer.sqlbean.EmCBuyerAddress)
     */
    @Override
    public Result deleteBuyerAddress(EmCBuyerAddress emCBuyerAddress) throws BaseException {
        Result result = new Result();
        try {
            emCBuyerAddressDao.deleteEmCBuyerAddress(emCBuyerAddress);
            result.setResult("删除收货地址成功!");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "删除收货地址失败!");
        }
        return result;
    }

    /*
     * (non-Javadoc)
     * @see
     * com.ym.b2b.buyer.service.EmCBuyerAddressService#searchPageBuyerAddress(com.ym.b2b.buyer.sqlbean.EmCBuyerAddress,
     * com.zc.commons.base.Page)
     */
    @Override
    public Result searchPageBuyerAddress(EmCBuyerAddress emCBuyerAddress) throws BaseException {
        Result result = new Result();

        try {
            result.setRows(emCBuyerAddressDao.searchEmCBuyerAddress(emCBuyerAddress));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "查询异常");
        }
        return result;
    }

    /**
     * 输入参数检查
     * 
     * @Title: checkInputBuyerAddress
     */
    private Result checkInputBuyerAddress(EmCBuyerAddress p) {
        Result result = new Result();
        if (StringUtils.isBlank(p.getAreaName())) {
            result.setCodeResult(Result.CODE_100, "缺少AreaName地区!");
            return result;
        }
        if (StringUtils.isBlank(p.getAreaCode())) {
            result.setCodeResult(Result.CODE_100, "缺少AreaCode地区!");
            return result;
        }
        if (StringUtils.isBlank(p.getAddress())) {
            result.setCodeResult(Result.CODE_100, "缺少address地址!");
            return result;
        }
        if (StringUtils.isBlank(p.getZip())) {
            result.setCodeResult(Result.CODE_100, "缺少zip邮编!");
            return result;
        }
        if (StringUtils.isBlank(p.getConsignee())) {
            result.setCodeResult(Result.CODE_100, "缺少consignee收货联系人!");
            return result;
        }
        if (StringUtils.isBlank(p.getPhone())) {
            result.setCodeResult(Result.CODE_100, "缺少phone收货联系电话!");
            return result;
        }
        if (p.getIsDefalut() == null) {
            result.setCodeResult(Result.CODE_100, "缺少isDefalut是否默认地址!");
            return result;
        }

        p.setUpdateDate(new Date());
        return result;

    }

}
